package com.huanke.iot.api.controller.h5.wb;

import com.huanke.iot.api.controller.h5.BaseController;
import com.huanke.iot.api.controller.h5.req.BaseRequest;
import com.huanke.iot.api.controller.h5.req.wb.*;
import com.huanke.iot.api.service.device.wb.MaintenanceService;
import com.huanke.iot.base.api.ApiResponse;
import com.huanke.iot.base.constant.RetCode;
import com.huanke.iot.base.dto.wb.MaintenanceOrderDetailVo;
import com.huanke.iot.base.dto.wb.MaintenanceOrderVo;
import com.huanke.iot.base.dto.wb.MaintenanceUserMsgVo;
import com.huanke.iot.base.dto.wb.MaintenanceWbUserVo;
import com.huanke.iot.base.request.wb.DistributeRequest;
import com.huanke.iot.base.request.wb.FeedbackRequest;
import com.huanke.iot.base.request.wb.MaintenanceManageLoginRequest;
import com.huanke.iot.base.request.wb.MaintenanceOrderRequest;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/h5/wb")
@RestController
@Slf4j
public class MaintenanceController  extends BaseController {
    @Autowired
    private MaintenanceService maintenanceService;

    /**
     * 管理员登陆
     * @return
     */
    @ApiOperation("管理员登陆")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ApiResponse<MaintenanceWbUserVo> login(@RequestBody MaintenanceManageLoginRequest request) {
        log.info("管理员登陆");
        Integer customerUserId = getCurrentUserId();
        Integer customerId = getCurrentCustomerId();
        MaintenanceWbUserVo userVo = maintenanceService.login(request, customerUserId, customerId);
        if(userVo == null){
            return new ApiResponse<>(500,"登陆失败");
        }
        return new ApiResponse<>(userVo);
    }

    /**
     * 提交报修订单
     * @return
     */
    @ApiOperation("业主提交报修订单")
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    public ApiResponse<String> addOrder(@RequestBody MaintenanceOrderRequest request) {
        log.info("业主提交报修订单");
        Integer customerUserId = getCurrentUserId();
        Integer customerId = getCurrentCustomerId();
        String manageUserPhone = maintenanceService.addOrder(request, customerUserId, customerId);
        return new ApiResponse<>(manageUserPhone);
    }

    /**
     * 业主查看订单
     * orderStatus  0:全部 1：待审核 2：待维修  3：以忽略
     * @return
     */
    @ApiOperation("业主查看全部订单")
    @RequestMapping(value = "/orderlist/{orderStatus}", method = RequestMethod.GET)
    public ApiResponse<List<MaintenanceOrderVo>> orderlist(@PathVariable Integer orderStatus) {
        log.info("查看全部订单,orderStatus->{}", orderStatus);
        Integer customerUserId = getCurrentUserId();
        List<MaintenanceOrderVo> orderlist = maintenanceService.orderlist(customerUserId, orderStatus);
        return new ApiResponse<>(orderlist);
    }

    /**
     * 维保人员查看订单
     * orderStatus  0:全部 1：待维修 2：待确认  3：以驳回  4：已完成
     * @return
     */
    @ApiOperation("维保人员查看订单")
    @RequestMapping(value = "/wbOrderlist/{orderStatus}", method = RequestMethod.GET)
    public ApiResponse<List<MaintenanceOrderVo>> wbOrderlist(@PathVariable Integer orderStatus) {
        log.info("查看全部订单,orderStatus->{}", orderStatus);
        Integer customerUserId = getCurrentUserId();
        List<MaintenanceOrderVo> orderlist = maintenanceService.wbOrderlist(customerUserId, orderStatus);
        return new ApiResponse<>(orderlist);
    }

    /**
     * 管理人员查看订单
     * orderStatus  0:全部 1：待分配 2：待维修  3：待审核  4：已驳回  5：已完成  6：已忽略
     * @return
     */
    @ApiOperation("管理人员查看订单")
    @RequestMapping(value = "/manageOrderlist/{orderStatus}", method = RequestMethod.GET)
    public ApiResponse<List<MaintenanceOrderVo>> manageOrderlist(@PathVariable Integer orderStatus) {
        log.info("维保人员查看全部订单,orderStatus->{}", orderStatus);
        List<MaintenanceOrderVo> orderlist = maintenanceService.manegeOrderlist(orderStatus);
        return new ApiResponse<>(orderlist);
    }

    /**
     * 维保人员维修工单
     * @return
     */
    @ApiOperation("维保人员维修工单")
    @RequestMapping(value = "/resultPhoto/{orderId}", method = RequestMethod.POST)
    public ApiResponse<Boolean> resultPhoto(@PathVariable Integer orderId, @RequestBody BaseRequest<List<String>> request) {
        log.info("维保人员维修工单,orderId->{}, resultPhoto->{}", orderId, request.getValue());
        Integer customerUserId = getCurrentUserId();
        maintenanceService.resultPhoto(orderId, request.getValue());
        return new ApiResponse<>(true);
    }

    /**
     * 用户标注解决结果
     * feedback 2：未解决 1：已解决
     * @return
     */
    @ApiOperation("用户标注解决结果")
    @RequestMapping(value = "/feedback", method = RequestMethod.POST)
    public ApiResponse<Boolean> feedback(@RequestBody FeedbackRequest feedbackRequest) {
        log.info("用户标注解决结果,orderId->{},feedbackStatus->{}", feedbackRequest.getOrderId(), feedbackRequest.getFeedback());
        Integer customerUserId = getCurrentUserId();
        maintenanceService.feedback(feedbackRequest, customerUserId);
        return new ApiResponse<>(true);
    }

    /**
     * 查看订单详情
     * @return
     */
    @ApiOperation("查看订单详情")
    @RequestMapping(value = "/orderDetail/{orderId}", method = RequestMethod.GET)
    public ApiResponse<MaintenanceOrderDetailVo> orderDetail(@PathVariable Integer orderId) {
        log.info("查看订单详情,orderId->{}", orderId);
        Integer currentCustomerId = getCurrentCustomerId();
        MaintenanceOrderDetailVo maintenanceOrderDetailVo = maintenanceService.orderDetail(orderId, currentCustomerId);
        return new ApiResponse<>(maintenanceOrderDetailVo);
    }

    /**
     * 评价
     * @return
     */
    @ApiOperation("评价")
    @RequestMapping(value = "/evaluation", method = RequestMethod.POST)
    public ApiResponse<Boolean> evaluation(@RequestBody EvaluationRequest evaluationRequest) {
        log.info("用户标注解决结果,orderId->{},star->{},evaluation->{}", evaluationRequest.getOrderId(), evaluationRequest.getStar(),evaluationRequest.getEvaluation());
        maintenanceService.evaluation(evaluationRequest.getOrderId(), evaluationRequest.getStar(), evaluationRequest.getEvaluation());
        return new ApiResponse<>(true);
    }




    /**
     * 编辑个人信息
     * @return
     */
    @ApiOperation("编辑个人信息")
    @RequestMapping(value = "/userMsg", method = RequestMethod.POST)
    public ApiResponse<Boolean> userMsg(@RequestBody UserMsgRequest userMsgRequest) {
        log.info("编辑个人信息,name->{},phone->{}", userMsgRequest.getName(), userMsgRequest.getPhone());
        Integer customerUserId = getCurrentUserId();
        Integer customerId = getCurrentCustomerId();
        maintenanceService.userMsg(customerId, customerUserId,userMsgRequest.getUserId(), userMsgRequest.getName(), userMsgRequest.getPhone());
        return new ApiResponse<>(true);
    }

    /**
     * 忽略
     * @return
     */
    @ApiOperation("忽略")
    @RequestMapping(value = "/neglect/{orderId}", method = RequestMethod.GET)
    public ApiResponse<Boolean> neglect(@PathVariable Integer orderId) {
        log.info("忽略,orderId->{}", orderId);
        maintenanceService.neglectOrder(orderId);
        return new ApiResponse<>(true);
    }

    /**
     * 管理员派单
     * @return
     */
    @ApiOperation("管理员派单")
    @RequestMapping(value = "/distribute", method = RequestMethod.POST)
    public ApiResponse<String> distribute(@RequestBody DistributeRequest distributeRequest) {
        log.info("管理员派单,orderId->{}，wbUserId->{}", distributeRequest.getOrderId(), distributeRequest.getWbUserId());
        Integer customerUserId = getCurrentUserId();
        String wbUserPhone = null;
        try {
            wbUserPhone = maintenanceService.distribute(distributeRequest, customerUserId);
        } catch (Exception e) {
            return new ApiResponse<>(RetCode.ERROR, e.getMessage());
        }
        return new ApiResponse<>(wbUserPhone);
    }

    /**
     * 管理员 已完成/已拒绝
     * @return
     */
    @ApiOperation("管理员 已完成/已拒绝")
    @RequestMapping(value = "/finish/{orderId}", method = RequestMethod.POST)
    public ApiResponse<Boolean> finish(@PathVariable("orderId") Integer orderId, @RequestBody BaseRequest<Integer> request) {
        log.info("管理员 已完成/已拒绝");
        Integer currentUserId = getCurrentUserId();
        maintenanceService.finish(orderId, request.getValue(), currentUserId);
        return new ApiResponse<>(true);
    }

    /**
     * 维护人员列表
     * @return
     */
    @ApiOperation("维护人员列表")
    @RequestMapping(value = "/obtainWbUsers", method = RequestMethod.POST)
    public ApiResponse<List<MaintenanceWbUserVo>> obtainWbUsers() {
        List<MaintenanceWbUserVo> maintenanceWbUserVos  = maintenanceService.obtainWbUsers();
        return new ApiResponse<>(maintenanceWbUserVos);
    }

    /**
     * 查询个人信息
     * @return
     */
    @ApiOperation("查询个人信息")
    @RequestMapping(value = "/getUserMsg", method = RequestMethod.GET)
    public ApiResponse<MaintenanceUserMsgVo> getUserMsg() {
        Integer customerUserId = getCurrentUserId();
        MaintenanceUserMsgVo maintenanceUserMsgVo = maintenanceService.getUserMsg(customerUserId);
        return new ApiResponse<>(maintenanceUserMsgVo);
    }

    /**
     * 查询个人信息
     * @return
     */
    @ApiOperation("查询维修个人信息")
    @RequestMapping(value = "/getWbUserMsg", method = RequestMethod.GET)
    public ApiResponse<MaintenanceUserMsgVo> getWbUserMsg() {
        Integer customerUserId = getCurrentUserId();
        MaintenanceUserMsgVo maintenanceUserMsgVo = maintenanceService.getWbUserMsg(customerUserId);
        return new ApiResponse<>(maintenanceUserMsgVo);
    }


}
